﻿/*
    This file is part of File Renamer.

    File Renamer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3.

    File Renamer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace FileRenamer
{
    class AlphabeticCounter
    {
        private Char[] Alphabet;
        private int startFrom;
        private int currentValue;

        public AlphabeticCounter(int startFrom)
        {
            this.startFrom = startFrom;
            this.currentValue = startFrom;
            Alphabet = new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        }

        public AlphabeticCounter(String startFrom)
        {
            this.startFrom = this.AlphabeticToNumeric(startFrom);
            this.currentValue = this.startFrom;

            Alphabet = new Char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        }

        private int AlphabeticToNumeric(String number)
        {
            int result = 0;

            for (int i = 0; i < number.Length; i++)
            {
            }

            return 0;
        }

        public String Next(PREFIX_SUFIX_TYPE nType)
        {
            if (nType == PREFIX_SUFIX_TYPE.ASC)
            {
                currentValue++;
            }
            else if (nType == PREFIX_SUFIX_TYPE.DES)
            {
                currentValue--;
            }
            return this.ConvertNumberToAlphabetic(currentValue);
        }

        private String ConvertNumberToAlphabetic( int number )
        {
            int m = number;
            int n = 26;//The base
            String result = "";

            if (m < n)
            {
                return result.Insert(0, Alphabet[m].ToString());
            }

            do
            {
                int mod = m % n;
                m /= n;                
                result = result.Insert(0, Alphabet[mod].ToString());

                if (m < n)
                {
                    result = result.Insert(0, Alphabet[m].ToString());
                }

            } while (m > n);

            return result;
        }
    }
}
